home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / introduction / example3.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  6KB  |  137 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Introduction                Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-09-24                                       */
  11. /* Version: 1.1                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* For experienced users only! This example demonstrates how */
  21. /* to create a long word aligned BPTR. The actual BPTR (the  */
  22. /* memory used to store the BPTR address in) is long word    */
  23. /* aligned. This is rarely needed since you normally only    */
  24. /* work with addresses to data blocks which have to be long  */
  25. /* word aligned. However, if you ever have to give AmigaDOS  */
  26. /* an actual BPTR (not a BPTR address, but the BPTR itself)  */
  27. /* you need to allocate it as described in this example.     */
  28.  
  29.  
  30.  
  31. /* Some advanced (and probably confusing) information about BCPL    */
  32. /* pointers:                                                        */
  33. /*                                                                  */
  34. /* When you are working with AmigaDOS you will often use variables  */
  35. /* which have been declared as BPTRs (BPTR and BSTR are defined in  */
  36. /* header file "dos/dos.h"). What you have to remember is that      */
  37. /* these pointers must point to long word aligned data and must be  */
  38. /* in BPCL form (four times smaller than normal C pointers).        */
  39. /*                                                                  */
  40. /* To decalre a BPTR simply write:                                  */
  41. /*                                                                  */
  42. /*   BPTR my_bcpl_pointer;                                          */
  43. /*                                                                  */
  44. /* When you work with AmigaDOS you will often call functions, for   */
  45. /* example Open(), which will return a BPTR (a BPCL address). The   */
  46. /* memory which this returned BPCL pointer points to will have      */
  47. /* been allocated by the function itsef and will therefore be long  */
  48. /* word aligned. The address (value) in the BPTR will therefore     */
  49. /* point to long word aligned memory, and can be used with          */
  50. /* functions which requires BCPL pointers.                          */
  51. /*                                                                  */
  52. /* Now comes the tricky part! The pointer itself (the memory used   */
  53. /* to store the address in) is NOT long word aligned when you       */
  54. /* declare it as described above. Normally this is not a problem    */
  55. /* since you usually only work with the address stored in the BPTR. */
  56. /* However, if you ever would have to give AmigaDOS a BPCL pointer  */
  57. /* (not the address in the pointer, but the pointer itself) you     */
  58. /* must make sure that the actual pointer (the memory used to store */
  59. /* the addresses in) is also long word aligned.                     */
  60.  
  61.  
  62.  
  63. /* Include the normal dos header file: */
  64. #include <libraries/dos.h>
  65.  
  66. /* Include memory definitions: (MEMF_ANY...) */
  67. #include <exec/memory.h>
  68.  
  69. /* Now we include the necessary function prototype files:         */
  70. #include <clib/dos_protos.h>       /* General dos functions...    */
  71. #include <clib/exec_protos.h>      /* System functions...         */
  72. #include <stdio.h>                 /* Std functions [printf()...] */
  73. #include <stdlib.h>                /* Std functions [exit()...]   */
  74.  
  75.  
  76.  
  77. /* Set name and version number: */
  78. UBYTE *version = "$VER: AmigaDOS/AmigaDOS/Example3 1.1";
  79.  
  80.  
  81.  
  82. /* Declared our own function(s): */
  83. int main( int argc, char *argv[] );
  84.  
  85.  
  86.  
  87. /* The main function: */
  88.  
  89. int main( int argc, char *argv[] )
  90. {
  91.   /* Declare a normal C pointer to the */
  92.   /* BPTR pointer we will allocate:    */
  93.   LONG *my_aligned_bptr;
  94.   
  95.  
  96.  
  97.   /* Allocate some memory for the aligned BPTR pointer: */
  98.   /* (The memory we allocate will be long word aligned. */
  99.   /* We allocate 4 bytes = 1 long.)                     */
  100.   my_aligned_bptr = AllocMem( sizeof( BPTR ),
  101.     MEMF_ANY | MEMF_CLEAR );
  102.  
  103.  
  104.   /* Have we successfully allocated the memory? */
  105.   if( !my_aligned_bptr )
  106.   {
  107.     /* Not enough memory! Inform the user and quit: */
  108.     printf( "Could not allocate enough memory!\n" );
  109.  
  110.     /* Exit with an error code: */
  111.     exit( 20 );
  112.   }
  113.  
  114.  
  115.   /* We have now allocated a long word aligned BPTR! */
  116.   /* Note that the "my_aligned_bptr" contains the C  */
  117.   /* address of the aligned BPTR pointer!            */
  118.  
  119.   /* You can now use the long word aligned BPTR...   */
  120.   printf( "We can now use the long word aligned BPTR!\n" );  
  121.  
  122.  
  123.  
  124.   /* Deallocate the long word aligned BPTR when you */
  125.   /* do not need it any more:                       */
  126.   FreeMem( my_aligned_bptr, sizeof( BPTR ) );
  127.  
  128.   /* Remember that you may not use the memory any */
  129.   /* more after you have deallocated it!          */
  130.   printf( "The long word aligned BPTR has been deallocated!\n" );
  131.  
  132.   /* The End! */
  133.   exit( 0 );
  134. }
  135.  
  136.  
  137.